package com.i360day.redis.constant;

import com.alibaba.fastjson.JSON;

/**
 * @description:
 * @author: liju.z
 * @create: 2020-02-14 15:23
 **/
public class RedisThreadLocalContextHolder {

    private static final ThreadLocal<Object> contextHolder = new ThreadLocal<>();

    public static void setObj(Object obj) {
        contextHolder.set(obj);
    }

    public static <T> T getObj(Class<T> clazz) {
        Object obj = contextHolder.get();
        String jsonString = JSON.toJSONString(obj);
        return JSON.parseObject(jsonString, clazz);
    }

    public static void clear() {
        contextHolder.remove();
    }

}
